package com.sec.android.app.myfiles.module.local.recent;

import android.content.Context;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.info.AppFeatures;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.StorageType;
import com.sec.android.app.myfiles.module.local.home.HomeMenuImp;
import com.sec.android.app.myfiles.module.shortcut.ShortcutUtils;
import com.sec.android.app.myfiles.navigation.NavigationManager;
import com.sec.android.app.myfiles.util.PreferenceUtils;
import com.sec.android.app.myfiles.util.UiUtils;

import java.util.ArrayList;

public class RecentMenuImp extends HomeMenuImp {

    public RecentMenuImp(Context context, StorageType supportStorage) {
        super(context, supportStorage);
    }

    @Override
    public void onPrepareNormalOptionsMenu(Menu menu) {
        super.onPrepareNormalOptionsMenu(menu);
        boolean possibleActionMode = NavigationManager.isPossibleToActionMode();

        setMenuItemVisibility(menu, R.id.menu_edit, possibleActionMode && mListItemCount > 0);
        setMenuItemVisibility(menu, R.id.menu_share, possibleActionMode && mFragment.getTotalCheckableFileCount() > 0);
        setMenuItemVisibility(menu, R.id.menu_clear_recent_history, mListItemCount > 0 && !AppFeatures.isTabletUIMode());
        setMenuItemVisibility(menu, R.id.menu_contact_us, isMyFilesHome() && UiUtils.checkSamsungMembersApp(mContext));
        setMenuItemVisibility(menu, R.id.menu_optimize_storage, UiUtils.supportOptimizeStorage(mContext) && isMyFilesHome());
        setMenuItemVisibility(menu, R.id.menu_toggle_list_type, mListItemCount > 0);
        setMenuItemEnabled(menu, R.id.menu_toggle_list_type, mListItemCount > 0);
        setMenuItemIcon(menu.findItem(R.id.menu_toggle_list_type));
        setMenuItemVisibility(menu, R.id.menu_search, mListItemCount > 0);
        setMenuItemVisibility(menu, R.id.menu_manage_shortcut, (AppFeatures.isTabletUIMode() || AppFeatures.isNote()) && isMyFilesHome() && NavigationManager.isPossibleToActionMode() && ShortcutUtils.getShortcutItemsCount(mContext) > 0);
    }

    @Override
    public void onPrepareEditOptionsMenu(Menu menu) {
        ArrayList<FileRecord> selectedItem = mFragment.getSelectedFile();
        if (selectedItem != null) {
            int selectedCount = selectedItem.size();

            if (mFragment.getActionModeType() == AbsMyFilesFragment.ActionModeType.EDIT_FILE_FOLDER_OPERATION) {
                setMenuItemVisibility(menu, R.id.menu_share, false);
            } else {
                setMenuItemVisibility(menu, R.id.menu_share, selectedCount > 0 && isSharableRecords(selectedItem));
            }
            setMenuItemVisibility(menu, R.id.menu_remove, selectedCount > 0);
            if (selectedCount == 1) {
                setMenuItemVisibility(menu, R.id.menu_add_shortcut, ShortcutUtils.isSupportShortcut(mContext, selectedItem.get(0)));
            } else {
                setMenuItemVisibility(menu, R.id.menu_add_shortcut, false);
            }
            setMenuItemVisibility(menu, R.id.menu_details, selectedCount > 0);
        }
    }

    @Override
    protected boolean homeAsUpEnabled() {
        if (isHomeRecent() && !mNavigationInfo.isLeftPanelDisabled()) {
            return super.homeAsUpEnabled();
        }
        return true;
    }

    @Override
    public String getActionBarTitle() {
        String ret = null;
        if (mFragment != null) {
            NavigationManager nm = NavigationManager.getInstance(mFragment.getProcessId());
            if (nm != null && isHomeRecent() && mNavigationInfo != null && !mNavigationInfo.isLeftPanelDisabled()) {
                ret = super.getActionBarTitle();
            } else {
                ret = mContext.getString(R.string.subtitle_recent);
            }
        }
        return ret;
    }

    private boolean isHomeRecent() {
        return ((AppFeatures.isNote() && UiUtils.getScreenState(mContext) == AppConstants.LANDSCAPE && !UiUtils.isInMultiWindow(mActivity))) || AppFeatures.isTabletUIMode();
    }

    @Override
    public boolean _onOptionsItemSelected(MenuItem item) {
        return super._onOptionsItemSelected(item);
    }

    @Override
    protected void createContextualMenuOnBackground(ContextMenu menu) {
        if (mFragment.getRecentItemCount() > 0) {
            if (NavigationManager.isPossibleToActionMode()) {
                menu.add(0, R.id.menu_edit, 0, mContext.getString(R.string.menu_edit));
            }
            menu.add(0, R.id.menu_share, 1, mContext.getString(R.string.menu_share));
            menu.add(0, R.id.menu_clear_recent_history, 2, mContext.getString(R.string.menu_clear_recent_history));
        }
        if (!PreferenceUtils.getShowHiddenFiles(mContext)) {
            menu.add(0, R.id.menu_show_hidden_files, 3, mContext.getString(R.string.menu_show_hidden_files));
        } else {
            menu.add(0, R.id.menu_hide_hidden_files, 3, mContext.getString(R.string.menu_hide_hidden_files));
        }
    }

    @Override
    protected void createContextualMenuOnLeftPanel(ContextMenu menu, FileRecord record) {
        menu.add(0, R.id.menu_clear_history, 0, mContext.getString(R.string.menu_clear_recent_history));
    }

    @Override
    protected void _createContextualMenuOnItems(ContextMenu menu, FileRecord record, boolean isForOneItem) {
        menu.add(0, R.id.menu_remove, 0, mContext.getString(R.string.menu_remove));
        if (NavigationManager.isPossibleToActionMode() && showShareMenu(record)) {
            menu.add(0, R.id.menu_share, 0, mContext.getResources().getString(R.string.menu_share));
        }
        if (isForOneItem && ShortcutUtils.isSupportShortcut(mContext, record)) {
            menu.add(0, R.id.menu_add_shortcut, 0, mContext.getString(R.string.add_shortcut));
        }
        menu.add(0, R.id.menu_details, 0, mContext.getString(R.string.menu_details));
    }

    @Override
    public boolean _onContextualItemSelected(MenuItem item, ArrayList<FileRecord> srcRecordList, FileRecord record, AbsMyFilesFragment fragment) {
        if (mFragment != null) {
            srcRecordList = new ArrayList<>();
            srcRecordList.add(record);
            return super._onContextualItemSelected(item, srcRecordList, record, mFragment);
        }
        return false;
    }
}
